﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace JWTAuthorize
{
    public class JwtToken
    {
        /// <summary>
        /// 生成JWT的Token
        /// </summary>
        /// <returns></returns>
        public static dynamic CreateJwtToken(Claim[] claims, CustomRequirement requirement)
        {
            var now = DateTime.UtcNow;
            var jwt = new JwtSecurityToken(
                issuer: requirement.Issuer,
                audience: requirement.Audience,
                claims: claims,
                notBefore: now,
                expires: now.Add(requirement.Expiration),
                signingCredentials: requirement.SigningCredentials
            );
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
            var responseJson = new
            {
                Status = true,
                access_token = encodedJwt,
                expires_in = requirement.Expiration.TotalMilliseconds,
                token_type = "Bearer"//类型为berarer
            };
            return responseJson;
        }
    }
}
